package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.BuildAndDefeatedCount;
import com.swz.dcrm.model.CustomerDefeated;
import com.swz.dcrm.model.DailyComeShop;
import com.swz.dcrm.model.DailyCustomer;
import com.swz.dcrm.model.DailyTarget;
import com.swz.dcrm.model.DailyTask;
import com.swz.dcrm.model.DeliveryAndCancleOrderCount;
import com.swz.dcrm.model.HardWork;
import com.swz.dcrm.model.MonthSummary;
import com.swz.dcrm.model.MonthlyGoal;
import com.swz.dcrm.model.MonthlyGraph;
import com.swz.dcrm.model.MonthlyStructure;
import com.swz.dcrm.model.MonthlyTarget;
import com.swz.dcrm.model.RankingList;
import com.swz.dcrm.model.RankingListModel;
import com.swz.dcrm.model.StatisticsOrder;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.List;

import retrofit2.Retrofit;

public class AnalyzeViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private  MediatorLiveData<BaseResponse<List<DeliveryAndCancleOrderCount>>> analyzeType5;
    private MediatorLiveData<BaseResponse<List<BuildAndDefeatedCount>>>analyzeType4;
    private MediatorLiveData<BaseResponse<RankingListModel>>saleRankingList;
    private MediatorLiveData<BaseResponse<List<HardWork>>> hardWorkingRankingList;
    private MediatorLiveData<BaseResponse<List<RankingList>>>cancelOrder;
    private MediatorLiveData<BaseResponse<List<CustomerDefeated>>>customerDefeated;
    private MediatorLiveData<BaseResponse<List<StatisticsOrder>>>statisticsOrder;
    private MediatorLiveData<BaseResponse<MonthSummary>>monthSummary;
    private MediatorLiveData<BaseResponse<DailyTarget>>dailyTarget;
    private MediatorLiveData<BaseResponse<DailyComeShop>>dailyComeShop;
    private MediatorLiveData<BaseResponse<DailyCustomer>>dailyCustomer;
    private MediatorLiveData<BaseResponse<DailyTask>>dailyTask;
    private MediatorLiveData<BaseResponse<MonthlyGoal>> monthlyGoal;
    private MediatorLiveData<BaseResponse<MonthlyTarget>>monthlyTarget;
    private MediatorLiveData<BaseResponse<MonthlyStructure>>monthlyStructure;
    private MediatorLiveData<BaseResponse<MonthlyGraph>>monthlyGraph;

    public AnalyzeViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public  MediatorLiveData<BaseResponse<List<DeliveryAndCancleOrderCount>>> getAnalyzeType5(String startTime, String endTime){
        analyzeType5=creatLiveData(analyzeType5);
        analyzeType5.setValue(null);
        dcrmApi.getDeliveryAndCancleOrderAnalyze(startTime,endTime)
                .enqueue(new CallBack<>(this,analyzeType5));
        return analyzeType5;
    }
    public  MediatorLiveData<BaseResponse<List<BuildAndDefeatedCount>>> getAnalyzeType4(String startTime, String endTime){
        analyzeType4=creatLiveData(analyzeType4);
        analyzeType4.setValue(null);
        dcrmApi.getBuildAndDefeatedAnalyze(startTime,endTime)
                .enqueue(new CallBack<>(this,analyzeType4));
        return analyzeType4;
    }
    public MediatorLiveData<BaseResponse<RankingListModel>> getSaleRankingList(String startTime,String endTime,int type){
        saleRankingList=creatLiveData(saleRankingList);
        if(saleRankingList.getValue()!=null){
            saleRankingList.setValue(null);
        }
        dcrmApi.getSaleRankingList(startTime,endTime,type).enqueue(new CallBack<>(this,saleRankingList));
        return saleRankingList;
    }

    public MediatorLiveData<BaseResponse<List<HardWork>>> getHardWorkingRankingList(String startTime,String endTime) {
        hardWorkingRankingList=creatLiveData(hardWorkingRankingList);
        dcrmApi.getHardWorkingRankingList(startTime,endTime).enqueue(new CallBack<>(this,hardWorkingRankingList));
        return hardWorkingRankingList;
    }

    public MediatorLiveData<BaseResponse<List<RankingList>>> getCancelOrder(String startTime,String endTime) {
        cancelOrder=creatLiveData(cancelOrder);
        dcrmApi.getCancelOrder(startTime,endTime).enqueue(new CallBack<>(this,cancelOrder));
        return cancelOrder;
    }

    public MediatorLiveData<BaseResponse<List<CustomerDefeated>>> getCustomerDefeated(String startTime,String endTime,long carModelId) {
        customerDefeated=creatLiveData(customerDefeated);

        if(customerDefeated.getValue()!=null){
            customerDefeated.setValue(null);
        }
        dcrmApi.getCustomerDefeated(startTime,endTime,carModelId).enqueue(new CallBack<>(this,customerDefeated));
        return customerDefeated;
    }

    public MediatorLiveData<BaseResponse<List<DeliveryAndCancleOrderCount>>> getAnalyzeType5() {
        analyzeType5=creatLiveData(analyzeType5);
        return analyzeType5;
    }

    public MediatorLiveData<BaseResponse<List<BuildAndDefeatedCount>>> getAnalyzeType4() {
        analyzeType4=creatLiveData(analyzeType4);
        return analyzeType4;
    }

    public MediatorLiveData<BaseResponse<List<StatisticsOrder>>> getStatisticsOrder() {
        statisticsOrder=creatLiveData(statisticsOrder);
        dcrmApi.getStatisticsOrder().enqueue(new CallBack<>(this,statisticsOrder));
        return statisticsOrder;
    }

    public MediatorLiveData<BaseResponse<MonthSummary>> getMonthSummary() {
        monthSummary=creatLiveData(monthSummary);
        dcrmApi.getMonthSummary().enqueue(new CallBack<>(this,monthSummary));
        return monthSummary;
    }
    public MediatorLiveData<BaseResponse<DailyTarget>>getDailyTarget(){
        dailyTarget=creatLiveData(dailyTarget);
        dcrmApi.getDailyTarget().enqueue(new CallBack<>(this,dailyTarget));
        return dailyTarget;
    }
    public MediatorLiveData<BaseResponse<DailyComeShop>>getDailyComeShop(){
        dailyComeShop=creatLiveData(dailyComeShop);
        dailyComeShop.setValue(null);
        dcrmApi.getDailyComeShop().enqueue(new CallBack<>(this,dailyComeShop));
        return dailyComeShop;
    }
    public MediatorLiveData<BaseResponse<DailyComeShop>>getDailyComeShopData(){
        dailyComeShop=creatLiveData(dailyComeShop);
        return dailyComeShop;
    }

    public MediatorLiveData<BaseResponse<DailyCustomer>> getDailyCustomer() {
        dailyCustomer=creatLiveData(dailyCustomer);
        dcrmApi.getDailyCustomer().enqueue(new CallBack<>(this,dailyCustomer));
        return dailyCustomer;
    }

    public MediatorLiveData<BaseResponse<DailyTask>> getDailyTask() {
        dailyTask=creatLiveData(dailyTask);
        dcrmApi.getDailyTask().enqueue(new CallBack<>(this,dailyTask));
        return dailyTask;
    }

    public MediatorLiveData<BaseResponse<MonthlyGoal>> getMonthlyGoal() {
        monthlyGoal=creatLiveData(monthlyGoal);
        dcrmApi.getMonthlyGoal().enqueue(new CallBack<>(this,monthlyGoal));
        return monthlyGoal;
    }
    public MediatorLiveData<BaseResponse<MonthlyTarget>> getMonthlyTarget(){
        monthlyTarget=creatLiveData(monthlyTarget);
        dcrmApi.getMonthlyTarget().enqueue(new CallBack<>(this,monthlyTarget));
        return monthlyTarget;
    }

    public MediatorLiveData<BaseResponse<MonthlyStructure>> getMonthlyStructure() {
        monthlyStructure=creatLiveData(monthlyStructure);
        dcrmApi.getMonthlyStructure().enqueue(new CallBack<>(this,monthlyStructure));
        return monthlyStructure;
    }

    public MediatorLiveData<BaseResponse<MonthlyGraph>> getMonthlyGraph() {
        monthlyGraph=creatLiveData(monthlyGraph);
        dcrmApi.getMonthlyGraph().enqueue(new CallBack<>(this,monthlyGraph));
        return monthlyGraph;
    }
}
